package com.jie.langlangmall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.jie.langlangmall.mapper.SmsHomeBrandMapper;
import com.jie.langlangmall.model.SmsHomeBrand;
import com.jie.langlangmall.model.SmsHomeBrandExample;
import com.jie.langlangmall.service.SmsHomeBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/25
 * @Email 3217998214@qq.com
 * @Description 首页品牌管理
 */
@Service
public class SmsHomeBrandServiceImpl implements SmsHomeBrandService {

    @Autowired
    private SmsHomeBrandMapper homeBrandMapper;

    /**
     * 分页查询首页推荐品牌
     *
     * @param brandName
     * @param recommendStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @Override
    public List<SmsHomeBrand> list(String brandName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        // TODO: 在删除或者更新品牌的时候，更新这张表
        PageHelper.startPage(pageNum, pageSize);
        SmsHomeBrandExample example = new SmsHomeBrandExample();
        SmsHomeBrandExample.Criteria criteria = example.createCriteria();
        if (!StrUtil.isEmpty(brandName)) {
            criteria.andBrandNameLike("%" + brandName + "%");
        }
        if (recommendStatus != null) {
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        example.setOrderByClause("sort desc");
        return homeBrandMapper.selectByExample(example);
    }

    /**
     * 添加推荐品牌
     *
     * @param homeBrandList
     */
    @Override
    public void create(List<SmsHomeBrand> homeBrandList) {
        for (SmsHomeBrand smsHomeBrand : homeBrandList) {
            smsHomeBrand.setRecommendStatus(1);
            smsHomeBrand.setSort(0);
            homeBrandMapper.insert(smsHomeBrand);
        }
    }

    /**
     * 修改排序
     *
     * @param id
     * @param sort
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeBrand homeBrand = new SmsHomeBrand();
        homeBrand.setId(id);
        homeBrand.setSort(sort);
        homeBrandMapper.updateByPrimaryKeySelective(homeBrand);
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        SmsHomeBrandExample example = new SmsHomeBrandExample();
        example.createCriteria().andIdIn(ids);
        homeBrandMapper.deleteByExample(example);
    }

    /**
     * 批量修改状态
     *
     * @param ids
     * @param recommendStatus
     */
    @Override
    public void updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        SmsHomeBrand homeBrand = new SmsHomeBrand();
        homeBrand.setRecommendStatus(recommendStatus);
        SmsHomeBrandExample example = new SmsHomeBrandExample();
        example.createCriteria().andIdIn(ids);
        homeBrandMapper.updateByExampleSelective(homeBrand, example);
    }
}
